package com.bra;

import java.io.File;
import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

import com.bra.ui.activity.MapActivity;
import com.bra.ui.base.BaseActivity;
import com.bra.ui.fragment.HomeFragment;
import com.bra.ui.fragment.SportsFragment;
import com.bra.ui.fragment.MineFragment;
import com.bra.ui.fragment.VenueFragment;
import com.bra.utils.CameraUtilsLister;
import com.bra.utils.ImageUtil;
import com.bra.utils.JupushUtil;
import com.bra.utils.SPHelper;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
/**
 * 主界面
 * @author lenovo
 *
 */
public class MainActivity extends BaseActivity {
	// 首页
	private static final String HOME = "HOME";
	// 场馆
	private static final String PLAYQUAN = "PLAYQUAN";
	// 活动
	private static final String HUODONG = "HUODONG";
	// 我的
	private static final String MINE = "MINE";
	public static boolean isForeground = false;
	public static final String MESSAGE_RECEIVED_ACTION = "com.bra.MESSAGE_RECEIVED_ACTION";
	public static final String KEY_MESSAGE = "message";
	public static final String KEY_TITLE = "title";
	public static final String KEY_EXTRAS = "extras";
	private MessageReceiver mMessageReceiver;
	private HashMap<String, Fragment> fragmentMap;

	private void findView() {
		initFragment();
		RadioGroup tab = (RadioGroup) findViewById(R.id.home_tab_radiogroup);
		tab.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				String key = null;
				switch (checkedId) {
				case R.id.home_radiobutton:
					key = HOME;
					break;
				case R.id.deal_radiobutton:
					key = PLAYQUAN;
					break;
				case R.id.custom_radiobutton:
					key = HUODONG;
					break;
				case R.id.inventory_raddiobutton:
					key = MINE;
					break;
				}
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.container, fragmentMap.get(key)).commit();
			}
		});

		tab.check(R.id.home_radiobutton);
	}
 /**
  * 初始化fragment
  */
	private void initFragment() {
		fragmentMap = new HashMap<String, Fragment>();
		fragmentMap.put(HOME, new HomeFragment());
		fragmentMap.put(PLAYQUAN, new VenueFragment());
		fragmentMap.put(HUODONG, new SportsFragment());
		fragmentMap.put(MINE, new MineFragment());
	}

	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_main, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		findView();
		registerMessageReceiver(); // used for receive msg
		MobclickAgent.setSessionContinueMillis(30000);
		AnalyticsConfig.enableEncrypt(true);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			MobclickAgent.onKillProcess(this);
			BraApplication.getInstance().exits();
			this.overridePendingTransition(0, 0);
			return true;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	private String name;
	private static final int TAKE_PICTURE = 0;
	private static final int CHOOSE_PICTURE = 1;
	private static final int PHOTOSIZE = 1024 * 1024;
	public static Bitmap m_bitmap;
	/**
	 * 用户头像
	 * */
	private File userfaceFile;
	public static String SDCARD = Environment.getExternalStorageDirectory()
			+ "/Android/data/evmanager/iamge/";
	public Uri originalUri;
	private CameraUtilsLister camera;

	@SuppressWarnings("static-access")
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		name = String.valueOf(System.currentTimeMillis());
		// name = "100852654";
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case TAKE_PICTURE:
				m_bitmap = camera.getRightBitmap();
				if (m_bitmap != null) {
					userfaceFile = new File(SDCARD + name + ".jpg");
					ImageUtil.savePhotoToSDCard(m_bitmap, SDCARD, name);
					new Thread(new Runnable() {
						@Override
						public void run() {
							SPHelper.saveString(MainActivity.this,
									"phone_name", name);
							SPHelper.saveBoolean(MainActivity.this, "is_up",
									true);
						}
					}).start();
				} else {
					Toast.makeText(MainActivity.this, "保存照片失败,请稍后重试", 0).show();
				}
				break;

			case CHOOSE_PICTURE:
				// 照片的原始资源地址
				originalUri = data.getData();
				originalUri = data.getData();
				m_bitmap = camera.getRightBitmap(originalUri);
				// 打印出图片的宽和高
				if (m_bitmap != null) {
					userfaceFile = new File(SDCARD + name + ".jpg");
					ImageUtil.savePhotoToSDCard(m_bitmap, SDCARD, name);
					new Thread(new Runnable() {
						@Override
						public void run() {
							SPHelper.saveString(MainActivity.this,
									"phone_name", name);
							SPHelper.saveBoolean(MainActivity.this, "is_up",
									true);
						}
					}).start();
				} else {
					Toast.makeText(MainActivity.this, "保存照片失败,请稍后重试", 0).show();
				}
				break;
			default:
				break;
			}
		}
	}
	@Override
	protected void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);		
		isForeground = true;						
	}


	@Override
	protected void onPause() {		
		super.onPause();
		JPushInterface.onPause(this);
		isForeground = false;		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mMessageReceiver);
	}
	private void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MESSAGE_RECEIVED_ACTION);
		registerReceiver(mMessageReceiver, filter);
	}
	public class MessageReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
				String messge = intent.getStringExtra(KEY_MESSAGE);
				String extras = intent.getStringExtra(KEY_EXTRAS);
				StringBuilder showMsg = new StringBuilder();
				showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
				if (!JupushUtil.isEmpty(extras)) {
					showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
				}
			/*	Intent msgIntent = new Intent(MESSAGE_RECEIVED_ACTION);
				MainActivity.this.sendBroadcast(msgIntent);*/
			}
		}
	}

}
